// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// 去除无用依赖
// const WorkboxWebpackPlugin = require('workbox-webpack-plugin');

/**
 * @description 引入copyWebpackPlugin,生产环境打包静态资源
 * @link https://www.cnblogs.com/ajaemp/p/13221284.html
 * @type {*|{}}
 */
const copyWebpackPlugin = require('copy-webpack-plugin');

const isProduction = process.env.NODE_ENV === 'production';


const stylesHandler = 'style-loader';


const config = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
    },
    devServer: {
        open: true,
        host: 'localhost',
        // 配置调试端口
        port: 8083
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html',
        }),

        // Add your plugins here
        // Learn more about plugins from https://webpack.js.org/configuration/plugins/

        // 配置插件打包静态资源
        new copyWebpackPlugin(
            {
                /*
                 * @link https://segmentfault.com/q/1010000022731396
                 * @description copy-webpack-plugin 从 6.0 以后添加了 patterns 字段。
                 */
                patterns: [
                    {
                        from: path.resolve(__dirname + '/static'),// 打包的静态资源目录地址
                        to: 'static', // 打包到dist下面的static
                        globOptions: {
                            /*
                             * @description 排除要打包的文件,可以配置多个 v9.0.0
                             * @link https://www.npmjs.com/package/copy-webpack-plugin
                             * @link https://blog.csdn.net/qq_43702430/article/details/106187706
                             * @type {Array}
                             */
                            ignore: [
                                '**/lib/jsencrypt-3.2.0.js'
                            ]
                        }
                    },
                    {
                        from: path.resolve(__dirname + '/README.md'),// 打包的静态资源目录地址
                        to: './README.md' // 打包到dist下面的README.md
                    },
                    {
                        from: path.resolve(__dirname + '/favicon.ico'),// 打包的静态资源目录地址
                        to: './favicon.ico' // 打包到dist下面的favicon.ico
                    }
                ]
            }
        )
    ],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/i,
                loader: 'babel-loader',
            },
            {
                test: /\.css$/i,
                use: [stylesHandler, 'css-loader'],
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
                type: 'asset',
            },

            // Add your rules for custom modules here
            // Learn more about loaders from https://webpack.js.org/loaders/
        ],
    },
};

module.exports = () => {
    if (isProduction) {
        config.mode = 'production';


        // 去除无用依赖
        // config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());

    } else {
        config.mode = 'development';
    }
    return config;
};
